package tech.aistar.service.impl;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tech.aistar.entity.ComVo;
import tech.aistar.entity.Com;
import tech.aistar.entity.UserInfo;
import tech.aistar.mapper.ComMapper;
import tech.aistar.service.IComService;
import tech.aistar.service.IComVoService;
import tech.aistar.service.IUserInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/10/9.
 */
@Service
@Transactional
public class ComVoServiceImpl implements IComVoService{
    @Autowired
    private IComService comService;
    @Autowired
    private IUserInfoService userInfoService;

    /**
     * 根据帖子id处查询所有的评论
     * 先将所有评论取出
     */
    @Override
    public List<ComVo> findAllByPostId(Integer postId) {
        List<Com> comList1 = comService.findOneComment(postId);
        //将上面的一级评论放入新建comVoList集合中
        List<ComVo> comVoList = new ArrayList<>();
        comList1.forEach(System.err::println);
        //遍历一级评论
        for (Com c:comList1){
            ComVo comVo = new ComVo();
            comVo.setComId(c.getComId());
            comVo.setFloor(c.getFloor());//楼层
            comVo.setPostId(c.getPostId());
            comVo.setUserId(c.getUserId());
            comVo.setToUserId(c.getToUserId());
            comVo.setContent(c.getContent());
            comVo.setCtime(c.getCtime());
            comVo.setPointNum(c.getPointNum());
            comVo.setFlag(c.getFlag());
            //遍历一级子评论
            List<Com> comList2 = comService.findAllTwoComment(c.getComId(),c.getPostId());
//            System.err.println("--------4444-------------");
//            System.err.println("--------44444------------");
            for (Com r:comList2){
                r.setUserInfo(userInfoService.findById(r.getUserId()));
                r.setToUserInfo(userInfoService.findById(r.getToUserId()));
            }
            if (comList2 != null){
                comVo.setTwocomList(comList2);
            }
            comVo.setUserInfo(userInfoService.findById(c.getUserId()));
            comVo.setToUserInfo(userInfoService.findById(c.getToUserId()));

            comVoList.add(comVo);
        }
        return comVoList;
    }

    @Override
    public Map<String, Object> findAllByPostId(Integer postId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);




        return null;
    }
}
